package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumFeignClient albumFeignClient;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private ThreadPoolExecutor executor;

    @Override
    public Map<String, Object> albumInfo(Long albumId) {

        Map<String, Object> resultMap = new ConcurrentHashMap<>();

        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo,"获取专辑信息异常,专辑id:{}",albumId);
            resultMap.put("albumInfo", albumInfo);
            return albumInfo;
        }, executor);

        CompletableFuture<Void> categoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(
                albumInfo -> {
                    BaseCategoryView categoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
                    Assert.notNull(categoryView,"获取分类信息异常,三级分类id:{}",albumInfo.getCategory3Id());
                    resultMap.put("baseCategoryView",categoryView);
                }, executor);

        CompletableFuture<Void> userCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(
                albumInfo -> {
                    UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
                    Assert.notNull(userInfoVo,"获取主播信息异常,用户id:{}",albumInfo.getUserId());
                    resultMap.put("announcer",userInfoVo);
                }, executor);

        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(
                () -> {
                    AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
                    Assert.notNull(albumStatVo,"获取专辑统计信息异常,专辑id:{}",albumId);
                    resultMap.put("albumStatVo",albumStatVo);
                }, executor);

        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                categoryCompletableFuture,
                userCompletableFuture,
                statCompletableFuture
        ).join();


        return resultMap;
    }
}
